package com.source.lightfowl.idcardgrantandrecovery.service.impl;

import com.source.lightfowl.VO.IdCardRecoveryVO;
import com.source.lightfowl.basedata.dao.IdCardDao;
import com.source.lightfowl.idcardgrantandrecovery.dao.IdCardRecoveryDao;
import com.source.lightfowl.idcardgrantandrecovery.entity.IdCardRecovery;
import com.source.lightfowl.idcardgrantandrecovery.service.IdCardRecoveryService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.dao.RoleDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.person.entity.Role;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 14:54
 */
@Service
public class IdCardRecoveryServiceImpl implements IdCardRecoveryService {
    @Autowired
    private IdCardRecoveryDao idCardRecoveryDao;
    @Autowired
    private IdCardDao idCardDao;
    @Autowired
    private PersonDao personDao;
    @Autowired
    private RoleDao roleDao;


    @Override
    public List<IdCardRecoveryVO> findAll() {
        List<IdCardRecovery> list = idCardRecoveryDao.findAll();

        List<IdCardRecoveryVO> collect = list.stream().map(s -> {
            IdCardRecoveryVO vo = new IdCardRecoveryVO();
            BeanUtils.copyProperties(s,vo);


            Person user = personDao.findVO(s.getRecoveryPerson()); //回收人
            if (user != null) {
                //回收人名字
                vo.setRecyclingName(user.getLoginName());
                //回收人角色名
                String roleId = user.getRole();
                String rname = roleDao.getOne(roleId).getRname();
                vo.setRecyclingRoteName(rname);
            }


            Person user1 = personDao.findVO(s.getSendBackPerson());
            if (user1 != null) {
                //送回人名字
                vo.setSendName(user1.getLoginName());
                String roleId = user1.getRole();
                String rname = roleDao.getOne(roleId).getRname();
                //送回人角色名
                vo.setSendRoteName(rname);
            }




            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public Page<IdCardRecovery> findAllPage(int page, int pageNum) {
        return idCardRecoveryDao.findAll(PageRequest.of(page-1,pageNum));
    }

    @Override
    public IdCardRecoveryVO findById(Integer id) {
        Optional<IdCardRecovery> byId = idCardRecoveryDao.findById(id);
        if (byId.isPresent()){
            IdCardRecovery s = byId.get();
            IdCardRecoveryVO vo = new IdCardRecoveryVO();
            BeanUtils.copyProperties(s, vo);

            Person user = personDao.findVO(s.getRecoveryPerson()); //回收人
            //
            if (user != null) {
                vo.setRecyclingName(user.getLoginName());
                //发放人角色名 （10.15版本的修改）
                String roleId = user.getRole();
                //String rname = roleDao.getOne(roleId).getRname();
                Optional<Role> byId1 = roleDao.findById(roleId);
                if (byId1.isPresent()){
                    vo.setRecyclingRoteName(byId1.get().getRname());
                }

            }
            //Person user1 = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getSendBackPerson()).longValue()));
            Person user1 = personDao.findVO(s.getSendBackPerson());
            //
            if (user1 != null) {
                vo.setSendName(user1.getLoginName());
                String roleId = user1.getRole();
                //String rname = roleDao.getOne(roleId).getRname();
                Optional<Role> optional = roleDao.findById(roleId);
                if (optional.isPresent()){
                    vo.setSendName(optional.get().getRname());
                }

            }

            return vo;
        }

        return new IdCardRecoveryVO();
    }

    @Override
    public void save(IdCardRecovery sh) {
        sh.setCreateDate(LocalDateTime.now().toString());
        //设置发放时间
        if (sh.getRecoveryDate() == null){
            sh.setRecoveryDate(LocalDateTime.now().toString());
        }
        idCardRecoveryDao.save(sh);
        String recoveryPersonId = sh.getRecoveryPerson();
        //修改user表外键
        personDao.updateIdCardNull(recoveryPersonId);
        //回收idcard状态回到待用
        idCardDao.updateRecovery(String.valueOf(sh.getCardId()));
        //回收暂无影响发放表
    }

    @Override
    public void deleteById(Integer id) {
        idCardRecoveryDao.deleteById(id);
    }

    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        idCardRecoveryDao.deleteBatch(list);
    }

    @Override
    public void update(IdCardRecovery sh) {
        sh.setCreateDate(LocalDateTime.now().toString());
        //设置发放时间
        if (sh.getRecoveryDate() == null){
            sh.setRecoveryDate(LocalDateTime.now().toString());
        }
        idCardRecoveryDao.save(sh);
    }

    @Override
    public Long getCount() {
        long count = idCardRecoveryDao.count();
        return count;
    }
}
